<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            let arr = [1, 2, 3];
            // console.log(arr.unshift(4, 5)); // 5
            // console.log(arr); // [4,5,1,2,3]

            Array.prototype.myUnshift = function () {
                // 最终数组的长度sum = 原数组长度 + unshift传入的参数的个数
                let sum = this.length + arguments.length;
                for (let i = sum; i > 0; i--) {
                    // 如果还没往后遍历到传入的参数的时候
                    // 原数组的值依次往向后赋值
                    if (i > arguments.length) {
                        this[i - 1] = this[i - 1 - arguments.length];
                    } else {
                        this[i - 1] = arguments[i - 1];
                    }
                }
                return sum;
            };
            console.log(arr.myUnshift(4, 5)); // 5
            console.log(arr); // [4,5,1,2,3]
        </script>
    </body>
</html>
